var express = require('express');
var router = express.Router();

var mysql=require('mysql');

var conn=mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'root',
	database:'node'
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

conn.connect();


router.get('/book', function(req, res, next) {
  
	conn.query('SELECT * from booklist', function(err, result) {
	    if (err){
			res.jsonp('404');
	    };
	    var len=result.length;
	    var shuju=[];
	    for(var i=0;i<len;i++){
	    	var liju={
	    		id:result[i].sid,
	    		title:result[i].name,
	    		images:result[i].images,
	    		txtle:result[i].txtle
	    	};
	    	shuju.push(liju);
	    };
	    res.jsonp(shuju);
	    
	});

});

router.get('/booklist', function(req, res, next) {
  	
  	var _url=req.url;
	var spA=_url.split('?')[1];
	var spB=spA.split('=')[1];
	var spC=spB.split('&')[0];

	conn.query('SELECT * from book'+spC, function(err, result) {
	    if (err){
			res.jsonp('404');
	    };
	    var len=result.length;
	    var shuju=[];
	    for(var i=0;i<len;i++){
	    	var liju={
	    		sid:result[i].id,
	    		title:result[i].title
	    	};
	    	shuju.push(liju);
	    };
	    res.jsonp(shuju);
	    
	});

});



router.get('/booklistshow', function(req, res, next) {
	var _url=req.url;
	var spA=_url.split('?')[1];

	//章节id
	var spB=spA.split('=')[2];
	var spC=spB.split('&')[0];

	//小说id
	var spD=spA.split('=')[1];
	var spE=spD.split('&')[0];

	console.log('SELECT * from book3510 where id = '+spC);
	conn.query('SELECT * from book'+spE+' where id = '+spC, function(err, result) {
	    if (err){
			res.jsonp('404');
	    };
	    res.jsonp(result);
	    
	});

});



router.get('/gaiwang', function(req, res, next) {

	conn.query('SELECT * from gaiwang', function(err, result) {
	    if (err){
			res.jsonp('404');
	    };
	    var len=result.length;
	    var shuju=[];
	    for(var i=0;i<len;i++){
	    	var liju={
	    		id:result[i].id,
	    		title:result[i].title,
	    		txtle:result[i].txtle,
	    		img:result[i].img,
	    		prie:result[i].prie
	    	};
	    	shuju.push(liju);
	    };
	    res.jsonp(shuju);
	    
	});

});

module.exports = router;
